package com.dy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.pojo.entity.DyTagEntity;
import com.dy.service.DyTagService;
import com.dy.utils.ApiResousUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/9 15:11
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/tag")
public class DyTagController {
    @Autowired
    private DyTagService dyTagService;

    /**
     * 标签分页查询
     * @return
     */
    @GetMapping("/queryTag")
    public Page query(@RequestParam(value="page",defaultValue = "1")Integer page,
                                                  @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize) {
        Page<DyTagEntity> dyTagEntityPage = dyTagService.queryTag(page, pageSize);
        return dyTagEntityPage;
    }

    /**
     * 添加
     * @param name
     * @return
     */
    @PostMapping("/createTag")
    public ApiResousUtil<String> createTag(@RequestParam("name")String name){
        if (name == null || name == "") {
            return ApiResousUtil.error("name为空");
        }
        dyTagService.createTag(name);
        return ApiResousUtil.ok();
    }
    /**
     * 删除标签
     * @param id
     * @return
     **/
    @GetMapping("/deleteTag")
    public ApiResousUtil<String> deleteTag(@RequestParam("id")Long id){
        if (id == null) {
            return ApiResousUtil.error("id为空");
        }
        dyTagService.deleteTag(id);
        return ApiResousUtil.ok();
    }
    /**
     * 根据id查询
     * @param id
     * @return
     **/
    @GetMapping("/selectTagById")
    public ApiResousUtil<DyTagEntity> selectTagById(@RequestParam("id")Long id){
        if (id == null) {
            return ApiResousUtil.error("id为空");
        }
        DyTagEntity dyTagEntity = dyTagService.selectTagById(id);
        return ApiResousUtil.ok(dyTagEntity);
    }
    /**
     * 修改
     * @param id
     * @param name
     * @return
     **/
    @PostMapping("/updateTag")
    public ApiResousUtil<String> updateTag(@RequestParam("id")Long id,@RequestParam("name")String name){
        if (id == null || name == null) {
            return ApiResousUtil.error("id或name为空");
        }
        dyTagService.updateTag(id,name);
        return ApiResousUtil.ok();
    }
    /**
     * 查询所有tag
     * @return
     **/
    @GetMapping("/selectTags")
    public ApiResousUtil selectTags(){
        List<DyTagEntity> listTags = dyTagService.list();
        return ApiResousUtil.ok(listTags);
    }
}
